#include "Camera.h"

CCamera *CCamera::Instance = new CCamera();

CCamera::CCamera(void)
{
	_ViewPortX = 0;
	_ViewPortY = 0;
	_IsAllowBack = false;
	_AutoMoveX = false;
	_AutoMoveY = false;
}

CCamera::~CCamera(void)
{
	
}

void CCamera::SetWorldSize(int WorldWidth, int WorldHeight)
{
	_WorldHeight = WorldHeight;
	_WorldWidth = WorldWidth;
}

void CCamera::Update(int ContraX, int ContraY)
{
	if(ContraX >= _WorldWidth - SCREEN_WIDTH)
		_AutoMoveX = true;
	if(_oldY >= _WorldWidth - SCREEN_WIDTH)
		_AutoMoveY = true;
	if(_IsAllowBack)
	{
			_ViewPortX = ContraX - SCREEN_WIDTH/2;

		if(ContraY >= _WorldHeight - SCREEN_HEIGHT)
			_ViewPortY += 2;
		else
			_ViewPortY = ContraY + SCREEN_HEIGHT/2;
	}
	else
	{
		if(!_AutoMoveX)
		{
			if(ContraX > _oldX)
			{
				_ViewPortX = ContraX - SCREEN_WIDTH/2;
				_oldX  = ContraX;
			}
		}
		else
		{
			_ViewPortX += 3;
		}

		if(!_AutoMoveY)
		{
			if(ContraY > _oldY)
			{
				_ViewPortY = ContraY + SCREEN_HEIGHT/2;
				_oldY = ContraY;
			}
		}
		else
		{
			_ViewPortY += 3;
		}
	}

	if (_ViewPortX <= 0)
	{
		_ViewPortX = 0;
	}
	else
	{
		if (_ViewPortX >= _WorldWidth - SCREEN_WIDTH)
		{
			_ViewPortX = _WorldWidth - SCREEN_WIDTH;
		}
	}
	
	if (_ViewPortY <= SCREEN_HEIGHT)
	{
		_ViewPortY = SCREEN_HEIGHT;
	}
	else
	{
		if (_ViewPortY >= _WorldHeight)
		{
			_ViewPortY = _WorldHeight;
		}
	}	
}

CCamera *CCamera::getInstance()
{
	return Instance;
}

D3DXVECTOR2 CCamera::GetViewPort()
{
	return D3DXVECTOR2(_ViewPortX, _ViewPortY);
}